home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-14 | 767 b | 35 lines | [TEXT/CWIE] |
- module: Stack
- author: Patrick C. Beard <beard@cs.ucdavis.edu>
- description: A simple LIFO stack.
-
- define module Stack
- use Dylan; // all programs need this.
- export
- <stack>,
- top;
- end module Stack;
-
- define class <stack> (<object>)
- slot %contents :: <list>,
- init-value: #();
- end class <stack>;
-
- define method push (stack :: <stack>, value) => (stack :: <stack>);
- stack.%contents := pair(value, stack.%contents);
- stack;
- end method;
-
- define method pop (stack :: <stack>) => (value :: <object>);
- let value = top(stack);
- stack.%contents := tail(stack.%contents);
- value;
- end method;
-
- define method top (stack :: <stack>) => (value :: <object>);
- if (~empty?(stack.%contents))
- head(stack.%contents);
- else
- error("stack is empty!");
- end if;
- end method;
-